<?php
function backup_db_tables($host, $user, $pass, $name, $tables = '*') {

    try {
        $link = mysql_connect($host, $user, $pass);
        mysql_select_db($name, $link);

//get all of the tables
        if ($tables == '*') {
            $tables = array();
            $result = mysql_query('SHOW TABLES');
            while ($row = mysql_fetch_row($result)) {
                $tables[] = $row[0];
            }
        } else {
            $tables = is_array($tables) ? $tables : explode(',', $tables);
        }

//cycle through
        foreach ($tables as $table) {
            $result = mysql_query('SELECT * FROM ' . $table);
            $num_fields = mysql_num_fields($result);

            $return.= 'DROP TABLE ' . $table . ';';
            $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
            $return.= "\n\n" . $row2[1] . ";\n\n";

            for ($i = 0; $i < $num_fields; $i++) {
                while ($row = mysql_fetch_row($result)) {
                    $return.= 'INSERT INTO ' . $table . ' VALUES(';
                    for ($j = 0; $j < $num_fields; $j++) {
                        $row[$j] = addslashes($row[$j]);
                        $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
                        if (isset($row[$j])) {
                            $return.= '"' . $row[$j] . '"';
                        } else {
                            $return.= '""';
                        }
                        if ($j < ($num_fields - 1)) {
                            $return.= ',';
                        }
                    }
                    $return.= ");\n";
                }
            }
            $return.="\n\n\n";
        }

//save file
        $handle = fopen('[prefix]-' . time() . '-' . (md5(implode(',', $tables))) . '.sql', 'w+');
        fwrite($handle, $return);
        fclose($handle);
        return 1;
    } catch (Exception $error_string) {
        return $error_string;
    }
}

function CalcFullDatabaseSize($database, $db) {

    $tables = mysql_list_tables($database, $db);
    if (!$tables) {
        return -1;
    }

    $table_count = mysql_num_rows($tables);
    $size = 0;

    for ($i = 0; $i < $table_count; $i++) {
        $tname = mysql_tablename($tables, $i);
        $r = mysql_query("SHOW TABLE STATUS FROM " . $database . " LIKE '" . $tname . "'");
        $data = mysql_fetch_array($r);
        $size += ($data['Index_length'] + $data['Data_length']);
    };

    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size > 1024; $i++) {
        $size /= 1024;
    }
    return round($size, 2) . $units[$i];
}

/**
   * @function run_commands
   * @abstract This is the function handler for all my data requests
   * @param $query string - is the prepare statement
   * @param $params array -is the parameters for the prepare statement
   * @param $command bool - true if it is an insert, update, delete statement. If false then its a select statement
   * @return 
   */
	function run_commands($query, $params, $command)
	{
		//declaring the default mode so long
		$result ="";
		
		//connecting to the database
		//setting to connect
		/*$hostname = "localhost";
		$username = "ODBC";
		$password = "password";
		$database = "test";*/
		
		//uploaded
		$hostname = "localhost";
		$username = "ODBC";
		$password = "password";
		$password = "";
		$database = "gms_db";
		//creating a new instance to the mySQL object
		$mysqli = new mysqli($hostname, $username, $password, $database);

		//checking for errors
		if(!mysqli_connect_errno())
		{
			$stmt = $mysqli->prepare($query);
			//call the function inside of the prepare statement and set all the parameters of the string
			if(count($params))
			{
				call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
			}
			
			$stmt->execute();
			//if true it is an UPDATE,INSERT or DELETE
			if($command)
			{
				//if successful then show the success message
				if($mysqli->affected_rows > 0)
				{
					$result = 'success';	
				}
				else
				{
					//Show the error
					$result = " " . $mysqli->error;
				}
			}
			//Means a select statement
			else
			{
				//creating columns for the data 
				$meta = $stmt->result_metadata();
            
               	while ($field = $meta->fetch_field())
				{
                   $parameters[] = &$row[$field->name];
               	}  
        
            	call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));				
				//getting the data, two deminsional array
				while ( $stmt->fetch() ) 
				{ 
               		$x = array(); 
			$results;
               		foreach( $row as $key => $val ) 
					{ 
						//creating a key so can get the values
                  		$x[$key] = $val; 
					} 
               		$results[] = $x; 
            	}
		if(!empty($results))
		{		
            		$result = $results;
		}
		else
		{
			$result = null;
		}
			}
			
			//$stmt->close();
			$mysqli->close();
		}
		else
		{
			$result = "** Connection to database was unsuccessful";
		}
		return  $result;
	}
?>